﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Verkefni5.Models;

namespace Verkefni5.Controllers
{
    public class CommentsController : ApiController
    {
        // GET api/comment
        public IEnumerable<Comment> Get(int LectureID)
        {

            AppDataContext db = new AppDataContext();
            var l = (from comment in db.Comments
                     where comment.Lecture.ID == LectureID
                     select comment);
            return l;
        }

        // GET api/comment/5
        /*public string Get(int id)
        {
            return "value";
        }*/

        // POST api/comment
        public void Post(int LectureID,Comment value)
        {
            AppDataContext db = new AppDataContext();
            var l = (from lectures in db.Lectures
                     where lectures.ID == LectureID
                     select lectures).SingleOrDefault();
            Comment c = new Comment();
            c.Lecture = l;
            c.UserID = "unknown";
            c.CommentText = value.CommentText;
            c.date = DateTime.Now;
            db.Comments.Add(c);
            db.SaveChanges();
        }

        // PUT api/comment/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/comment/5
        public void Delete(int id)
        {
        }
    }
}
